package util;


import java.sql.*;
import java.util.ResourceBundle;

/**
 * 数据库 SqlServerUtils
 */
public class SqlUtils {
	private static final String DRIVERCLASS; // 驱动
	private static final String URL; // 地址
	private static final String USERNAME; // 用户名
	private static final String PASSWORD; // 密码

	static {
		DRIVERCLASS = ResourceBundle.getBundle("SqlServerJdbc").getString("driverClass");
		URL = ResourceBundle.getBundle("SqlServerJdbc").getString("url");
		USERNAME = ResourceBundle.getBundle("SqlServerJdbc").getString("username");
		PASSWORD = ResourceBundle.getBundle("SqlServerJdbc").getString("password");
	}

	static {
		try {
			System.out.println(DRIVERCLASS);
			Class.forName(DRIVERCLASS);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws SQLException {

		// 2.获取连接
		Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		// Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);

		return con;
	}

	// 关闭操作
	public static void closeConnection(Connection con) throws SQLException {
		if (con != null) {
			con.close();
		}
	}

	public static void closeStatement(Statement st) throws SQLException {
		if (st != null) {
			st.close();
		}
	}

	public static void closeResultSet(ResultSet rs) throws SQLException {
		if (rs != null) {
			rs.close();
		}
	}
}

